﻿using System;
using System.Linq;
using SuperKnowledgeDatabase2000Client.KennismanagementReference;
using System.Collections;
using System.Collections.Generic;

namespace SuperKnowledgeDatabase2000Client
{
    public class Helper
    {
        public Helper()
        {
        }

        /// <summary>
        ///  merges 2 stringArray[] in alphabetical order
        /// </summary>
        /// <param name="first"></param>
        /// <param name="second"></param>
        /// <returns></returns>
        public stringArray[] mergeCategoriesAlphabetical(stringArray[] first, stringArray[] second)
        {
            stringArray[] current;
            if (first == null && second == null)
            {
                return new stringArray[0];
            }
            else if (first == null)
            {
                return sortCategoriesAlphabetical(second);
            }
            else if (second == null)
            {
                return sortCategoriesAlphabetical(first);
            }
            else
            {
                current = new stringArray[first.Length + second.Length];
            }
            int[] doubleEntries;
            int counter = 0;
            if (first.Length <= second.Length)
            {
                doubleEntries = new int[second.Length];
            }
            else
            {
                doubleEntries = new int[first.Length];
            }
            for (int i = 0; i < doubleEntries.Length; i++)
            {
                doubleEntries[i] = -1;
            }
            for (int i = 0; i < current.Length; i++)
            {
                current[i] = new stringArray();
            }
            for (int i = 0; i < first.Length; i++)
            {
                current[i].item = first[i].item;
            }
            for (int i = 0; i < first.Length; i++)
            {
                for (int j = 0; j < second.Length; j++)
                {
                    if (current[i].item.ElementAt(0) == second[j].item.ElementAt(0) && current[i].item.ElementAt(1) == second[j].item.ElementAt(1))
                    {
                        doubleEntries[counter] = j;
                        counter++;
                    }
                }
            }
            int added = 0;
            for (int i = 0; i < second.Length; i++)
            {
                if (!doubleEntries.Contains(i))
                {
                    current[first.Length + added].item = second[i].item;
                    added++;
                }
            }
            current = sortCategoriesAlphabetical(current);
            return current;
        }


        /// <summary>
        ///  orders a stringArray[] alphabetically
        /// </summary>
        /// <param name="toBeSorted"></param>
        /// <returns></returns>
        public stringArray[] sortCategoriesAlphabetical(stringArray[] toBeSorted)
        {
            stringArray[] current = new stringArray[0]; 
            if (toBeSorted == null)
            {
                return current;
            }
            if (toBeSorted.Length > 1)
            {
                int lastItem = -1;

                for (int i = 0; i < toBeSorted.Length; i++)
                {
                    if (toBeSorted[i].item == null)
                    {
                        lastItem = i;
                        break;
                    }
                }
                if (lastItem <= -1)
                {
                    lastItem = toBeSorted.Length;
                }
                current = new stringArray[lastItem];
                for (int i = 0; i < lastItem; i++)
                {
                    current[i] = toBeSorted[i];
                }
                for (int i = 0; i < lastItem; i++)
                {
                    for (int j = 0; j < lastItem - 1; j++)
                    {
                        if (current[j].item.ElementAt(1).CompareTo(current[j + 1].item.ElementAt(1)) > 0)
                        {
                            stringArray temp = current[j + 1];

                            current[j + 1] = current[j];

                            current[j] = temp;
                        }
                    }
                }
            }
            else
            {
                current = toBeSorted;
            }
            return current;
           
        }

        /// <summary>
        /// merges the categories in alphabetical order
        /// </summary>
        /// <param name="first"></param>
        /// <param name="second"></param>
        /// <param name="displayOutdatedFiles"></param>
        /// <returns></returns>
        public stringArray[] mergeListViewItemsAlphabetical(stringArray[] first, stringArray[] second, bool displayOutdatedFiles)
        {
            if (first != null && second != null && first.Count() > 0 && second.Count() > 0)
            {
                stringArray[] current = new stringArray[first.Length + second.Length];
                int[] doubleEntries = new int[first.Length + second.Length];
                int counter = 0;
                for (int i = 0; i < doubleEntries.Length; i++)
                {
                    doubleEntries[i] = -1;
                }
                for (int i = 0; i < current.Length; i++)
                {
                    current[i] = new stringArray();
                }
                for (int i = 0; i < first.Length; i++)
                {
                    current[i].item = first[i].item;
                }
                for (int i = 0; i < first.Length; i++)
                {
                    for (int j = 0; j < second.Length; j++)
                    {
                        if (displayOutdatedFiles)
                        {
                            if (current[i].item.ElementAt(0) == second[j].item.ElementAt(0) && current[i].item.ElementAt(1) == second[j].item.ElementAt(1) && current[i].item.ElementAt(2) == second[j].item.ElementAt(2) && current[i].item.ElementAt(3) == second[j].item.ElementAt(3) && current[i].item.ElementAt(4) == second[j].item.ElementAt(4) && current[i].item.ElementAt(5) == second[j].item.ElementAt(5) && current[i].item.ElementAt(6) == second[j].item.ElementAt(6) && current[i].item.ElementAt(7) == second[j].item.ElementAt(7) && current[i].item.ElementAt(8) == second[j].item.ElementAt(8))
                            {
                                doubleEntries[counter] = i;
                                counter++;
                            }
                        }
                        else
                        {
                            if (current[i].item.ElementAt(0) == second[j].item.ElementAt(0) && current[i].item.ElementAt(1) == second[j].item.ElementAt(1) && current[i].item.ElementAt(2) == second[j].item.ElementAt(2) && current[i].item.ElementAt(4) == second[j].item.ElementAt(4) && current[i].item.ElementAt(5) == second[j].item.ElementAt(5) && current[i].item.ElementAt(6) == second[j].item.ElementAt(6) && current[i].item.ElementAt(7) == second[j].item.ElementAt(7) && current[i].item.ElementAt(8) == second[j].item.ElementAt(8))
                            {
                                doubleEntries[counter] = i;
                                counter++;
                            }
                        }
                    }
                }
                int added = 0;
                for (int i = 0; i < second.Length; i++)
                {
                    if (!doubleEntries.Contains(i))
                    {
                        current[first.Length + added].item = second[i].item;
                        added++;
                    }
                }
                current = sortListViewItemsAlphabetical(current);
                return current;
            }
            else if (first == null && second == null)
            {
                return null;
            }
            else if (first == null && second != null)
            {
                return sortListViewItemsAlphabetical(second);
            }
            else 
            {
                return sortListViewItemsAlphabetical(first);
            }
        }


        /// <summary>
        ///  orders a stringArray[] alphabetically
        /// </summary>
        /// <param name="toBeSorted"></param>
        /// <returns></returns>
        public stringArray[] sortListViewItemsAlphabetical(stringArray[] toBeSorted)
        {
            stringArray[] current;
            if (toBeSorted.Length > 1)
            {
                int lastItem = -1;

                for (int i = 0; i < toBeSorted.Length; i++)
                {
                    if (toBeSorted[i].item == null)
                    {
                        lastItem = i;
                        break;
                    }
                }
                if (lastItem <= -1)
                {
                    lastItem = toBeSorted.Length;
                }
                current = new stringArray[lastItem];
                int[] jUsed = new int[lastItem];
                for (int i = 0; i < jUsed.Length; i++)
                {
                    jUsed[i] = -1;
                }
                stringArray currentItem = null;

                for (int i = 0; i < current.Length; i++)
                {
                    for (int j = 0; j < current.Length; j++)
                    {

                        if (!jUsed.Contains(j))
                        {
                            if (currentItem == null || (currentItem.item.ElementAt(6).CompareTo(toBeSorted[j].item.ElementAt(6)) > 0))
                            {
                                currentItem = toBeSorted[j];
                                jUsed[i] = j;
                            }
                        }

                    }
                    current[i] = currentItem;
                    currentItem = null;
                }
                return current;
            }
            else
            {
                return toBeSorted;
            }
        }

        /// <summary>
        /// handles the view of the categories and removes categories with a certain type
        /// </summary>
        /// <param name="source"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public stringArray[] removeItemsByType(stringArray[] source, string type)
        {

            if (type == "information" || type == "problem")
            {
                string currentType = "";
                if (type == "information")
                {
                    currentType = "Information";
                }
                else if (type == "problem")
                {
                    currentType = "Problem";
                }
                ArrayList list = new ArrayList();
                for (int i = 0; i < source.Count(); i++)
                {
                    if (source[i].item.ElementAt(7) == currentType)
                    {
                        list.Add(i);
                    }
                }
                int counter = 0;
                stringArray[] current = new stringArray[list.Count];
                for (int i = 0; i < source.Count(); i++)
                {
                    if (list.Contains(i))
                    {
                        current[counter] = source[i];
                        counter++;
                    }
                }
                return current;
            }
            else
            {
                return source;
            }
            
        }
    }
}
